<?php

namespace App\Admin\Controllers;

use App\Models\Area;
use App\Models\Complaint;
use App\Models\Credit;
use DB;
use Encore\Admin\Controllers\HasResourceActions;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Layout\Content;
use App\Http\Controllers\Controller;

class HandleController extends Controller
{
    use HasResourceActions;

    /**
     * Index interface.
     *
     * @return Content
     */
    public function index()
    {
        return Admin::content(function (Content $content) {

            $content->header('旅游投诉');

            $content->body($this->grid());
        });
    }

    /**
     * Edit interface.
     *
     * @param $id
     * @return Content
     */
    public function edit($id)
    {
        return Admin::content(function (Content $content) use ($id) {

            $content->body($this->form()->edit($id));
        });
    }


    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Admin::grid(Complaint::class, function (Grid $grid) {
            $grid->actions(function ($actions) {
                $actions->disableView();
            });
            $grid->disableCreateButton();
            $grid->disableExport();
            $grid->model()->where('status', 1);
            $grid->id('ID')->sortable();
            $grid->name('投诉人');
            $grid->behavior('投诉行为')->display(function ($val){
                return config('options.complaint_behavior')[$val];
            });
            $grid->phone('联系电话');
            $grid->status()->radio(config('options.complaint_status'));
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Admin::form(Complaint::class, function (Form $form) {
            $form->tab('投诉信息',function ($form){
                $form->display('area_id','区域')->with(function ($value) {
                    return $value?Area::all()->pluck('title','id')[$value]:'';
                });
                $form->display('type','事件类型')->with(function ($value) {
                    return config('options.complaint_type')[$value];
                });
                $form->display('target','投诉类型')->with(function ($value) {
                    return config('options.credit_module')[$value];
                });
                $form->display('target_id','投诉对象')->with(function ($value) {
                    $data = DB::table($this->target)->find((int)$value);
                    return $value?($this->model=='guides'?$data->name:$data->title):'';
                });
                $form->display('behavior','投诉行为')->with(function ($value) {
                    return $value?config('options.complaint_behavior')[$value]:'';
                });
                $form->display('other','其他')->help('投诉行为为其他时选填此项');
                $form->display('name','投诉人');
                $form->display('phone','联系电话');
                $form->display('cause','投诉事由/诉求');
                $form->display('time','投诉日期');
            })->tab('申诉处理',function ($form){
                $form->textarea('appeal','申诉/辩解');
                $form->textarea('reply_answer','回复');
                $form->radio('event_tracking','投诉跟踪')->options(config('options.complaint_event_tracking'));
                $form->date('tracking_time','日期');
            })->tab('投诉处理',function ($form){
                $form->text('handle_person','值班人员');
                $form->text('enforcement_number','执法证编号');
                $form->textarea('result','处理结果');
                $form->date('handle_time','处理日期');
                $form->select('result_status','游客满意度')->options(config('options.complaint_result'));
                $form->radio('status','状态')->options(config('options.complaint_status'));
            });
            $form->saved(function (Form $form){
                $model = $form->model()->target;
                $target_id = $form->model()->target_id;
                $year =  date('Y',strtotime($form->model()->time));
                $month = date('m',strtotime($form->model()->time));
                if($form->model()->status==2){
                    Credit::create([
                        'module'=>$model,
                        'target_id'=>$target_id,
                        'total'=>100,
                        'year'=>$year,
                        'month'=>$month
                    ]);
                }
            });
        });
    }
}
